package com.vonzhou.spitter.persistence;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class SimpleJdbcTemplateSpitterDao implements SpitterDao {

	private static final String SQL_INSERT_SPITTER =
			"insert into spitter (username, password, fullname, email, update_by_email) values (?, ?, ?, ?, ?)";
	private static final String SQL_UPDATE_SPITTER =
			"update spitter set username = ?, password = ?, fullname = ?, set email=?"
					+ "where id = ?";
	private static final String SQL_SELECT_SPITTER = ""
			+ "select id, username, password, fullname from spitter";
	private static final String SQL_SELECT_SPITTER_BY_ID = SQL_SELECT_SPITTER
			+ " where id=?";
	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public Spitter getSpitterById(long id) {
		return jdbcTemplate.queryForObject(
				SQL_SELECT_SPITTER_BY_ID,
				new ParameterizedRowMapper<Spitter>() {
					@Override
					public Spitter mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Spitter spitter = new Spitter();
						spitter.setId(rs.getLong(1));
						spitter.setUsername(rs.getString(2));
						spitter.setPassword(rs.getString(3));
						spitter.setFullName(rs.getString(4));
						return spitter;
					}
				},
				id
		);
	}

	@Override
	public void addSpitter(Spitter spitter) {
		jdbcTemplate.update(SQL_INSERT_SPITTER,
				spitter.getUsername(),
				spitter.getPassword(),
				spitter.getFullName(),
				spitter.getEmail(),
				spitter.isUpdateByEmail());
		System.out.println("Counts:" + queryForIdentity());
//        spitter.setId(queryForIdentity());
	}

	@Override
	public void saveSpitter(Spitter spitter) {
		jdbcTemplate.update(SQL_UPDATE_SPITTER,
				spitter.getUsername(),
				spitter.getPassword(),
				spitter.getFullName(),
				spitter.getEmail(),
				spitter.getId());
	}

	@Override
	public List<Spittle> getRecentSpittle() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void saveSpittle(Spittle spittle) {
		// TODO Auto-generated method stub

	}


	// queryForLong的参数是一sql语句
	private long queryForIdentity() {
		return jdbcTemplate.queryForLong("select count(*) from spitter;");
	}

	@Override
	public List<Spittle> getSpittlesForSpitter(
			Spitter spitter) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Spitter getSpitterByUsername(String username) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deleteSpittle(long id) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Spittle getSpittleById(long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Spitter> findAllSpitters() {
		// TODO Auto-generated method stub
		return null;
	}
}
